<template>
	<view>
		<view class="wrap" v-if="loading">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" @transition="transition" @animationfinish="animationfinish" :lower-threshold="0"
				:scroll-top="scrolltop">
				<swiper-item class="swiper-item" v-if="orderList.length">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="ReachBottom" v-if="orderType!=1">
						<view class="page-box">
							<view class="order" @click="pathUrl(res.id)" v-for="(res, index) in orderList" :key="res.id">
								<view class="top">
									<view class="left">
										<u-icon name="clock" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">{{ res.time }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<view class="right">{{ res.orderstatus }}</view>
								</view>
								<view class="item" v-for="(item, index) in res.list" :key="index">
									<view class="left">
										<image :src="item.image.split(',')[0]" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ item.name }}</view>
										<view class="type">{{ item.attributeList }}</view>
										<!-- <view class="delivery-time">发货时间 {{ item.deliveryTime }}</view> -->
									</view>
									<view class="right">
										<view class="price">
											￥{{ item.price }}
											<!-- <text class="decimal">.{{ priceDecimal(item.price) }}</text> -->
										</view>
										<view class="number">x{{ item.num }}</view>
									</view>
									
								</view>
								<view class="total">
									<!-- 共{{ totalNum(res.goodsList) }}件商品 合计: -->
									<text class="total-price">
										<!-- ￥{{ priceInt(totalPrice(res.goodsList)) }}. -->
										<!-- <text class="decimal">{{ priceDecimal(totalPrice(res.goodsList)) }}</text> -->
									</text>
								</view>
								<view class="bottom">
									<!-- <view class="more"><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon>
									</view> -->
									<view class="logistics btn" @click.stop="modifyorder(res)" v-if="res.status==0">取消订单
									</view>
									<!-- <view class="exchange btn">修改订单</view> -->
									<view class="evaluate btn" @click.stop="pay(res)" v-if="res.status==0">立即支付</view>
									<view class="evaluate btn" @click.stop="pay(res)" v-if="res.status==2">查看物流</view>
								</view>
							</view>
							<u-loadmore :status="totalstatus" bgColor="#f2f2f2"></u-loadmore>
						</view>
						<!-- <view class="page-box">
							<view>
								<view class="centre text-center" style="width: 100%;">
									<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
									</image>
									<view class="explain">
										您还没有相关的订单
										<view class="tips">可以去看看有那些想买的</view>
									</view>
									<view class="btn">随便逛逛</view>
								</view>
							</view>
						</view> -->
					</scroll-view>
				</swiper-item>

				<swiper-item class="swiper-item" v-else>
					<u-empty text="暂无订单" iconSize="200" mode="order" class="flex justify-center align-center">
					</u-empty>
				</swiper-item>
				<!-- <swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in  orderList[3]" :key="res.id">
								<view class="top">
									<view class="left">
										<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">{{ res.store }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<view class="right">{{ res.deal }}</view>
								</view>
								<view class="item" v-for="(item, index) in res.goodsList" :key="index">
									<view class="left">
										<image :src="item.goodsUrl" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ item.title }}</view>
										<view class="type">{{ item.type }}</view>
										<view class="delivery-time">发货时间 {{ item.deliveryTime }}</view>
									</view>
									<view class="right">
										<view class="price">
											￥{{ priceInt(item.price) }}
											<text class="decimal">.{{ priceDecimal(item.price) }}</text>
										</view>
										<view class="number">x{{ item.number }}</view>
									</view>
								</view>
								<view class="total">
									共{{ totalNum(res.goodsList) }}件商品 合计:
									<text class="total-price">
										￥{{ priceInt(totalPrice(res.goodsList)) }}.
										<text class="decimal">{{ priceDecimal(totalPrice(res.goodsList)) }}</text>
									</text>
								</view>
								<view class="bottom">
									<view class="more"><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon>
									</view>
									<view class="logistics btn">查看物流</view>
									<view class="exchange btn">卖了换钱</view>
									<view class="evaluate btn">评价</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[3]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item> -->
			</swiper>
		</view>
		<u-empty text="暂无数据" iconSize="200" mode="data" class="flex justify-center align-center" v-else>
			<u-button slot="bottom" v-if="token" size="medium" type="primary" text="确定" style="margin:0 30upx;"
				@click="tologin()">
				点击登录
			</u-button>
		</u-empty>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderList: [],
				dataList: [],
				list: [{
						name: '全部',
						val: ''
					},
					{
						name: '待支付',
						val: '0'
					},
					{
						name: '待发货',
						val: '1'
					},
					{
						name: '待收货',
						val: '2'
					},
					// {
					// 	name: '待评价',
					// 	count: 12
					// }
				],
				current: 1,
				scrolltop: 0,
				searchVal: {
					pageIndex: 1,
					pageSize: 10,
					member:uni.getStorageSync('userInfo').id?uni.getStorageSync('userInfo').id:'00000000-0000-0000-0000-000000000000',
					orderstatus: '0',
					sorting: 'CreationTime desc',
				},
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				total: 0,
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore'],
				loading: false,
				totalstatus: 'nomore',
				orderType: 0
			};
		},
		onLoad(e) {
			let result = 0
			if (e.type) {
				this.list.forEach((v, index) => {
					if (v.val == e.type) {
						result = index
					}

				})
			}
			this.swiperCurrent = e.type || "";
			this.current = result || 0
			this.searchVal.orderstatus = e.type || ""
			if (e.status==1) {
				this.orderType = e.status
				this.list = [{
						name: '全部',
						val: ''
					},
					{
						name: '待支付',
						val: '0'
					},
					{
						name: '已支付',
						val: '0'
					},
				]
			}else {
				this.searchVal.member=uni.getStorageSync('userInfo').id
			}
			this.getData()

		},
		onPullDownRefresh() {
			this.pageIndex = 1
			this.getData()
		},
		computed: {
			// 价格小数
			priceDecimal() {
				return val => {
					if (val !== parseInt(val)) return val.slice(-2);
					else return '00';
				};
			},
			// 价格整数
			priceInt() {
				return val => {
					if (val !== parseInt(val)) return val.split('.')[0];
					else return val;
				};
			}
		},
		methods: {
			ReachBottom() {
				if (this.total <= this.orderList.length) {
					this.totalstatus = 'nomore'
				} else {
					this.searchVal.pageSize += 10
					this.totalstatus = 'loading'
					this.getData()
				}
			},
			getData() {
				let api = this.orderType==1 ? 'GetFellowInfo' : 'getallorder'
				this.$ajax[api](this.searchVal).then(res => {
					this.loading = true
					this.total = res.total
					if(res.data==[]){
						this.orderList=[]
						this.totalstatus = 'nomore'
					}else {
						this.orderList=this.searchVal.pageIndex==1?[...res.data]:[...this.orderList, ...res.data],
						this.totalstatus = 'loadmore'
						this.scrolltop = 150
					}
					this.$forceUpdate()
					uni.stopPullDownRefresh()
				})
			},
			pathUrl(val) {
				uni.navigateTo({
					url: `/pages/order/submitOrder/submitOrder?id=${val}&type=${2}`
				})
			},
			tologin(){
				uni.reLaunch({
					url:'/pages/home/login/login'
				})
			},
			// 页面数据
			// getOrderList(idx) {
			// 	// for (let i = 0; i < 5; i++) {
			// 	// 	let index = this.$u.random(0, this.dataList.length - 1);
			// 	// 	let data = JSON.parse(JSON.stringify(this.dataList[index]));
			// 	// 	data.id = this.$u.guid();
			// 	// 	this.orderList[idx].push(data);
			// 	// }
			// 	console.log(this.idx);
			// 	this.loadStatus.splice(this.current, 1, "loadmore")
			// },
			// 总价
			totalPrice(item) {
				let price = 0;
				item.map(val => {
					price += parseFloat(val.price);
				});
				return price.toFixed(2);
			},
			// 总件数
			totalNum(item) {
				let num = 0;
				item.map(val => {
					num += val.number;
				});
				return num;
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
				this.current = index
				this.orderList = []
				this.list.forEach((val, index1) => {
					if (index == index1) {
						this.searchVal.orderstatus = val.val
					}
				})
				this.getData()
				// this.getOrderList(index);
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			},
			// 立即支付
			pay(val) {
				let OpenId = uni.getStorageSync('userInfo').openId
				let data = JSON.parse(val.pay.pay)
				data.OpenId = OpenId
				this.$ajax.MiniProgramPay(data).then(result => {
					uni.requestPayment({
						provider: 'wxpay',
						...result.data,
						success: function(res) {
							this.getData()
						},
					})
				})

			},
			modifyorder(val) {
				let data = {
					OrderNo: val.orderNo,
					Orderstatus: '4'
				}
				this.$ajax.modifyorder(data).then(res => {
					uni.showToast({
						icon: 'none',
						title: res.message,
						duration: 2000
					})
					this.getData()
				})
			}
		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
					width: 400rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: flex-end;
			// box-direction: reverse;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
				margin-left: 20rpx;
			}

			.evaluate {
				color: white;
				background: linear-gradient(90deg, #FE6035, #EF1224);
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>